package com.viphome.controller;

import com.alibaba.fastjson.JSON;
import com.viphome.domain.Girl;
import com.viphome.repostitory.GirlRepository;
import com.viphome.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by wangzhanjin on 2017/12/23.
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /**
     * 查询女生列表所有
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList(){
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     * @param cupSize
     * @param age
     * @return
     */
//    @PostMapping(value = "/girlsAdd")
    @GetMapping(value = "/girlsAdd")
    //http://localhost:8080/girlsAdd?cupSize=dd&age=2
    public String girlAdd(@RequestParam("cupSize") String cupSize,
                          @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);
        girlRepository.save(girl);
        return JSON.toJSONString(girlRepository.findAll());
    }

    /**
     * 查询一个女生
     * @param id
     * @return
     */
    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return girlRepository.findOne(id);
    }

    @GetMapping(value = "/girls/age/{age}")
//    http://localhost:8080/girls/age/20
    public List<Girl> girlFindByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    /**
     * 更新一个女生，post
     * @param id
     * @return
     */
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                            @RequestParam("cupSize") String cupSize,
                            @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setId(id);
        girl.setCupSize(cupSize);
        return girlRepository.save(girl);
    }

//    @DeleteMapping,返回空，delete(id)



    //——————————————————————————————————————————————

    @GetMapping(value = "/girls/two")
    public void girlTwo(){
        girlService.insertTwo();
    }
}
